home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / dkbsrc.zip / OBJECTS.C < prev    next >
C/C++ Source or Header  |  1991-05-04  |  13KB  |  434 lines

  1. /*****************************************************************************
  2. *
  3. *                                    objects.c
  4. *
  5. *   from DKBTrace (c) 1990  David Buck
  6. *
  7. *  This module implements the methods for objects and composite objects.
  8. *
  9. * This software is freely distributable. The source and/or object code may be
  10. * copied or uploaded to communications services so long as this notice remains
  11. * at the top of each file.  If any changes are made to the program, you must
  12. * clearly indicate in the documentation and in the programs startup message
  13. * who it was who made the changes. The documentation should also describe what
  14. * those changes were. This software may not be included in whole or in
  15. * part into any commercial package without the express written consent of the
  16. * author.  It may, however, be included in other public domain or freely
  17. * distributed software so long as the proper credit for the software is given.
  18. *
  19. * This software is provided as is without any guarantees or warranty. Although
  20. * the author has attempted to find and correct any bugs in the software, he
  21. * is not responsible for any damage caused by the use of the software.  The
  22. * author is under no obligation to provide service, corrections, or upgrades
  23. * to this package.
  24. *
  25. * Despite all the legal stuff above, if you do find bugs, I would like to hear
  26. * about them.  Also, if you have any comments or questions, you may contact me
  27. * at the following address:
  28. *
  29. *     David Buck
  30. *     22C Sonnet Cres.
  31. *     Nepean Ontario
  32. *     Canada, K2H 8W7
  33. *
  34. *  I can also be reached on the following bulleton boards:
  35. *
  36. *     OMX              (613) 731-3419
  37. *     Mystic           (613) 596-4249  or  (613) 596-4772
  38. *
  39. *  Fidonet:   1:163/109.9
  40. *  Internet:  dbuck@ccs.carleton.ca
  41. *  The "You Can Call Me RAY" BBS    (708) 358-5611
  42. *
  43. *  IBM Port by Aaron A. Collins. Aaron may be reached on the following BBS'es:
  44. *
  45. *     The "You Can Call Me RAY" BBS (708) 358-5611
  46. *     The Information Exchange BBS  (708) 945-5575
  47. *
  48. *****************************************************************************/
  49.  
  50.  
  51. #include "frame.h"
  52. #include "vector.h"
  53. #include "dkbproto.h"
  54.  
  55. extern RAY *VP_Ray;
  56. extern long Bounding_Region_Tests, Bounding_Region_Tests_Succeeded;
  57.  
  58. METHODS Composite_Methods =
  59.    { Object_Intersect, All_Composite_Intersections,
  60.      Inside_Composite_Object, NULL,
  61.      Copy_Composite_Object,
  62.      Translate_Composite_Object, Rotate_Composite_Object,
  63.      Scale_Composite_Object, Invert_Composite_Object};
  64.  
  65. METHODS Basic_Object_Methods =
  66.    { Object_Intersect, All_Object_Intersections,
  67.      Inside_Basic_Object, NULL,
  68.      Copy_Basic_Object,
  69.      Translate_Basic_Object, Rotate_Basic_Object,
  70.      Scale_Basic_Object, Invert_Basic_Object};
  71.  
  72.  
  73. INTERSECTION *Object_Intersect (Object, Ray)
  74.    OBJECT *Object;
  75.    RAY *Ray;
  76.    {
  77.    INTERSECTION *Local_Intersection, *Queue_Element;
  78.    PRIOQ *Depth_Queue;
  79.  
  80.    Depth_Queue = pq_new (128);
  81.  
  82.    if ((All_Intersections (Object, Ray, Depth_Queue))
  83.       && ((Queue_Element = pq_get_highest (Depth_Queue)) != NULL))
  84.       {
  85.       if ((Local_Intersection = (INTERSECTION *) malloc(sizeof(INTERSECTION)))
  86.               == NULL) {
  87.           printf("Cannot allocate memory for local intersection\n");
  88.           exit(1);
  89.           }
  90.       Local_Intersection->Point = Queue_Element->Point;
  91.       Local_Intersection->Shape = Queue_Element->Shape;
  92.       Local_Intersection->Depth = Queue_Element->Depth;
  93.       Local_Intersection->Object = Queue_Element->Object;
  94.       pq_free (Depth_Queue);
  95.       return (Local_Intersection);
  96.       }
  97.    else
  98.       {
  99.       pq_free (Depth_Queue);
  100.       return (NULL);
  101.       }
  102.    }
  103.  
  104. int All_Composite_Intersections (Object, Ray, Depth_Queue)
  105.    OBJECT *Object;
  106.    RAY *Ray;
  107.    PRIOQ *Depth_Queue;
  108.    {
  109.    register int Intersection_Found;
  110.    SHAPE *Bounding_Shape;
  111.    INTERSECTION *Local_Intersection;
  112.    OBJECT *Local_Object;
  113.  
  114.    for (Bounding_Shape = ((COMPOSITE *) Object) -> Bounding_Shapes ;
  115.         Bounding_Shape != NULL ;
  116.         Bounding_Shape = Bounding_Shape -> Next_Object) {
  117.  
  118.      Bounding_Region_Tests++;
  119.      if ((Local_Intersection = Intersection ((OBJECT *) Bounding_Shape, Ray)) != NULL)
  120.         free (Local_Intersection);
  121.      else
  122.         if (!Inside (&Ray -> Initial, (OBJECT *) Bounding_Shape))
  123.            return (FALSE);
  124.      Bounding_Region_Tests_Succeeded++;
  125.      }
  126.  
  127.    Intersection_Found = FALSE;
  128.    for (Local_Object = ((COMPOSITE *) Object) -> Objects ;
  129.         Local_Object != NULL ;
  130.         Local_Object = Local_Object -> Next_Object)
  131.  
  132.       if (All_Intersections (Local_Object, Ray, Depth_Queue))
  133.          Intersection_Found = TRUE;
  134.  
  135.    return (Intersection_Found);
  136.    }
  137.  
  138. int All_Object_Intersections (Object, Ray, Depth_Queue)
  139.    OBJECT *Object;
  140.    RAY *Ray;
  141.    PRIOQ *Depth_Queue;
  142.    {
  143.    INTERSECTION *Local_Intersection;
  144.    SHAPE *Bounding_Shape;
  145.  
  146.    for (Bounding_Shape = Object -> Bounding_Shapes ;
  147.         Bounding_Shape != NULL ;
  148.         Bounding_Shape = Bounding_Shape -> Next_Object) {
  149.  
  150.       Bounding_Region_Tests++;
  151.       if ((Local_Intersection = Intersection ((OBJECT *) Bounding_Shape, Ray)) != NULL)
  152.          free (Local_Intersection);
  153.       else
  154.          if (!Inside (&Ray -> Initial, (OBJECT *) Bounding_Shape))
  155.             return (FALSE);
  156.       Bounding_Region_Tests_Succeeded++;
  157.       }
  158.  
  159.    All_Intersections ((OBJECT *)Object->Shape, Ray, Depth_Queue);
  160.  
  161.    if (pq_is_empty (Depth_Queue))
  162.      return (FALSE);
  163.    return (TRUE);
  164.    }
  165.  
  166.  
  167. int Inside_Basic_Object (Test_Point, Object)
  168.    VECTOR *Test_Point;
  169.    OBJECT *Object;
  170.    {
  171.    SHAPE *Bounding_Shape;
  172.  
  173.    for (Bounding_Shape = Object -> Bounding_Shapes ;
  174.         Bounding_Shape != NULL ;
  175.         Bounding_Shape = Bounding_Shape -> Next_Object)
  176.  
  177.       if (!Inside (Test_Point, (OBJECT *) Bounding_Shape))
  178.          return (FALSE);
  179.  
  180.    if (Inside (Test_Point, (OBJECT *) Object -> Shape))
  181.       return (TRUE);
  182.    return (FALSE);
  183.    }
  184.  
  185. int Inside_Composite_Object (Test_Point, Object)
  186.    VECTOR *Test_Point;
  187.    OBJECT *Object;
  188.    {
  189.    SHAPE *Bounding_Shape;
  190.    OBJECT *Local_Object;
  191.  
  192.    for (Bounding_Shape = ((COMPOSITE *) Object) -> Bounding_Shapes ;
  193.         Bounding_Shape != NULL ;
  194.         Bounding_Shape = Bounding_Shape -> Next_Object)
  195.  
  196.      if (!Inside (Test_Point, (OBJECT *) Bounding_Shape))
  197.        return (FALSE);
  198.  
  199.    for (Local_Object = ((COMPOSITE *) Object) -> Objects ;
  200.         Local_Object != NULL ;
  201.         Local_Object = Local_Object -> Next_Object)
  202.  
  203.       if (Inside (Test_Point, Local_Object))
  204.          return (TRUE);
  205.  
  206.    return (FALSE);
  207.    }
  208.  
  209. void *Copy_Basic_Object (Object)
  210.    OBJECT *Object;
  211.    {
  212.    SHAPE *Local_Shape, *Copied_Shape;
  213.    OBJECT *New_Object;
  214.  
  215.    New_Object = Get_Object();
  216.    *New_Object = *Object;
  217.    New_Object -> Next_Object = NULL;
  218.    New_Object -> Bounding_Shapes = NULL;
  219.    for (Local_Shape = Object -> Bounding_Shapes ;
  220.         Local_Shape != NULL ;
  221.         Local_Shape = Local_Shape -> Next_Object) {
  222.  
  223.       Copied_Shape = (SHAPE *) Copy((OBJECT *) Local_Shape);
  224.       Link ((OBJECT *) Copied_Shape,
  225.             (OBJECT **) &(Copied_Shape -> Next_Object),
  226.             (OBJECT **) &(New_Object -> Bounding_Shapes));
  227.  
  228.       if ((Copied_Shape->Type == CSG_UNION_TYPE)
  229.           || (Copied_Shape->Type == CSG_INTERSECTION_TYPE)
  230.           || (Copied_Shape->Type == CSG_DIFFERENCE_TYPE))
  231.          Set_CSG_Parents ((CSG_SHAPE *) Copied_Shape, New_Object);
  232.       }
  233.  
  234.    New_Object -> Shape = (SHAPE *) Copy((OBJECT *) Object -> Shape);
  235.    if ((New_Object->Shape->Type == CSG_UNION_TYPE)
  236.        || (New_Object->Shape->Type == CSG_INTERSECTION_TYPE)
  237.        || (New_Object->Shape->Type == CSG_DIFFERENCE_TYPE))
  238.       Set_CSG_Parents ((CSG_SHAPE *) New_Object->Shape, New_Object);
  239.    else
  240.       New_Object->Shape->Parent_Object = New_Object;
  241.    
  242.  
  243.    if (New_Object->Object_Texture != NULL)
  244.       New_Object->Object_Texture = Copy_Texture (New_Object->Object_Texture);
  245.  
  246.    return (New_Object);
  247.    }
  248.  
  249. void *Copy_Composite_Object (Object)
  250.    OBJECT *Object;
  251.    {
  252.    COMPOSITE *New_Object;
  253.    SHAPE *Local_Shape;
  254.    OBJECT *Local_Object, *Copied_Object;
  255.  
  256.    New_Object = Get_Composite_Object();
  257.    *New_Object = *((COMPOSITE *) Object);
  258.    New_Object -> Next_Object = NULL;
  259.    New_Object -> Objects = NULL;
  260.    for (Local_Object = ((COMPOSITE *) Object) -> Objects;
  261.         Local_Object != NULL ;
  262.         Local_Object = Local_Object -> Next_Object) {
  263.  
  264.       Copied_Object = (OBJECT *) Copy(Local_Object);
  265.       Link (Copied_Object,
  266.             &(Copied_Object -> Next_Object),
  267.             &(New_Object -> Objects));
  268.       }
  269.  
  270.    New_Object -> Bounding_Shapes = NULL;
  271.    for (Local_Shape = ((COMPOSITE *) Object) -> Bounding_Shapes;
  272.         Local_Shape != NULL ;
  273.         Local_Shape = Local_Shape -> Next_Object) {
  274.  
  275.       Copied_Object = (OBJECT *) Copy((OBJECT *) Local_Shape);
  276.       Link (Copied_Object,
  277.             &(Copied_Object -> Next_Object),
  278.             (OBJECT **) &(New_Object -> Bounding_Shapes));
  279.       }
  280.    return (New_Object);
  281.    }
  282.  
  283. void Translate_Basic_Object (Object, Vector)
  284.    OBJECT *Object;
  285.    VECTOR *Vector;
  286.    {
  287.    SHAPE *Local_Shape;
  288.  
  289.    for (Local_Shape = Object -> Bounding_Shapes ;
  290.         Local_Shape != NULL ;
  291.         Local_Shape = Local_Shape -> Next_Object)
  292.  
  293.       Translate ((OBJECT *) Local_Shape, Vector);
  294.  
  295.    Translate ((OBJECT *) Object -> Shape, Vector);
  296.  
  297.    VAdd (Object -> Object_Center, Object -> Object_Center, *Vector);
  298.  
  299.    Translate_Texture (&Object->Object_Texture, Vector);
  300.    }
  301.  
  302. void Rotate_Basic_Object (Object, Vector)
  303.    OBJECT *Object;
  304.    VECTOR *Vector;
  305.    {
  306.    SHAPE *Local_Shape;
  307.    TRANSFORMATION Transformation;
  308.  
  309.    for (Local_Shape = Object -> Bounding_Shapes ;
  310.         Local_Shape != NULL ;
  311.         Local_Shape = Local_Shape -> Next_Object)
  312.  
  313.       Rotate ((OBJECT *) Local_Shape, Vector);
  314.  
  315.    Rotate ((OBJECT *) Object -> Shape, Vector);
  316.    Get_Rotation_Transformation (&Transformation, Vector);
  317.    MTransformVector (&Object->Object_Center,
  318.                      &Object->Object_Center, &Transformation);
  319.  
  320.    Rotate_Texture (&Object->Object_Texture, Vector);
  321.    }
  322.  
  323. void Scale_Basic_Object (Object, Vector)
  324.    OBJECT *Object;
  325.    VECTOR *Vector;
  326.    {
  327.    SHAPE *Local_Shape;
  328.  
  329.    for (Local_Shape = Object -> Bounding_Shapes ;
  330.         Local_Shape != NULL ;
  331.         Local_Shape = Local_Shape -> Next_Object)
  332.  
  333.       Scale ((OBJECT *) Local_Shape, Vector);
  334.  
  335.    Scale ((OBJECT *) Object -> Shape, Vector);
  336.  
  337.    VEvaluate (Object -> Object_Center, Object -> Object_Center, *Vector);
  338.  
  339.    Scale_Texture (&Object->Object_Texture, Vector);
  340.    }
  341.  
  342. void Translate_Composite_Object (Object, Vector)
  343.    OBJECT *Object;
  344.    VECTOR *Vector;
  345.    {
  346.    OBJECT *Local_Object;
  347.    SHAPE *Local_Shape;
  348.  
  349.    for (Local_Object = ((COMPOSITE *) Object) -> Objects;
  350.         Local_Object != NULL ;
  351.         Local_Object = Local_Object -> Next_Object)
  352.  
  353.       Translate (Local_Object, Vector);   
  354.  
  355.    for (Local_Shape = ((COMPOSITE *) Object) -> Bounding_Shapes ;
  356.         Local_Shape != NULL ;
  357.         Local_Shape = Local_Shape -> Next_Object)
  358.  
  359.       Translate ((OBJECT *) Local_Shape, Vector);
  360.    }
  361.  
  362. void Rotate_Composite_Object (Object, Vector)
  363.    OBJECT *Object;
  364.    VECTOR *Vector;
  365.    {
  366.    OBJECT *Local_Object;
  367.    SHAPE *Local_Shape;
  368.  
  369.    for (Local_Object = ((COMPOSITE *) Object) -> Objects;
  370.         Local_Object != NULL ;
  371.         Local_Object = Local_Object -> Next_Object)
  372.  
  373.       Rotate (Local_Object, Vector);   
  374.  
  375.    for (Local_Shape = ((COMPOSITE *) Object) -> Bounding_Shapes ;
  376.         Local_Shape != NULL ;
  377.         Local_Shape = Local_Shape -> Next_Object)
  378.  
  379.       Rotate ((OBJECT *) Local_Shape, Vector);
  380.    }
  381.  
  382. void Scale_Composite_Object (Object, Vector)
  383.    OBJECT *Object;
  384.    VECTOR *Vector;
  385.    {
  386.    OBJECT *Local_Object;
  387.    SHAPE *Local_Shape;
  388.  
  389.    for (Local_Object = ((COMPOSITE *) Object) -> Objects;
  390.         Local_Object != NULL ;
  391.         Local_Object = Local_Object -> Next_Object)
  392.  
  393.       Scale (Local_Object, Vector);   
  394.  
  395.    for (Local_Shape = ((COMPOSITE *) Object) -> Bounding_Shapes ;
  396.         Local_Shape != NULL ;
  397.         Local_Shape = Local_Shape -> Next_Object)
  398.  
  399.       Scale ((OBJECT *) Local_Shape, Vector);
  400.    }
  401.  
  402.  
  403. void Invert_Basic_Object (Object)
  404.    OBJECT *Object;
  405.    {
  406.    SHAPE *Local_Shape;
  407.  
  408.    for (Local_Shape = Object -> Bounding_Shapes ;
  409.         Local_Shape != NULL ;
  410.         Local_Shape = Local_Shape -> Next_Object)
  411.  
  412.       Invert ((OBJECT *) Local_Shape);
  413.    Invert ((OBJECT *) Object -> Shape);
  414.    }
  415.  
  416. void Invert_Composite_Object (Object)
  417.    OBJECT *Object;
  418.    {
  419.    OBJECT *Local_Object;
  420.    SHAPE *Local_Shape;
  421.  
  422.    for (Local_Object = ((COMPOSITE *)Object) -> Objects;
  423.         Local_Object != NULL ;
  424.         Local_Object = Local_Object -> Next_Object)
  425.  
  426.       Invert (Local_Object);   
  427.  
  428.    for (Local_Shape = ((COMPOSITE *) Object) -> Bounding_Shapes ;
  429.         Local_Shape != NULL ;
  430.         Local_Shape = Local_Shape -> Next_Object)
  431.  
  432.       Invert ((OBJECT *) Local_Shape);
  433.    }
  434.